
var imageurl="",offsetflag=0;
var GLOBALS  = new Array();
var instance = new Array(); 
var CACHEDIMAGE=null,actual=new Array(),IMAGEPATH=PATH+FILE;
var page_properties=null;

function scroll_reset() { // scroll_reset
	rollups ();
    opendebug ()
	if (MM_findObj("Dx")) {
		MM_findObj("Dx").scrollLeft = SCROLLLEFT;
		if (SCROLLTOP.length>0)  MM_findObj("Dx").scrollTop = SCROLLTOP;
		scroll_display()
	}
	if (MM_findObj("textform")) {		 
		refreshEditor ()		 
        desktop_background_resize();		 
		// MM_findObj("dhtml").scrollLeft = SCROLLLEFT;
		if (SCROLLTOP.length>0)  MM_findObj("dhtml").scrollTop = SCROLLTOP;
		MM_findObj("textform").save = savetextform;
	} //  else	sizepic()
	
	// TO DO: ONLY USE WHEN TREEVIEW IS SHOWN
	// locatesplitters (); 
	 
	page_properties = pagevars(); 
	
} // scroll_reset 


function render_drawing_grid () { // render_drawing_grid
	var d=MM_findObj,M=d("mainpic"),E=d("editworkspace"),mm=mboundary(M)
	
	GRID_SIZE=50;
	if (window.cached_image_size) { 
		GRID_SIZE=50/(window.cached_image_size/mm[4]);
	} else window.cached_image_size=mm[4];
	
	
	html = "<table cellspacing=0 cellpadding=0 border=1 style='border-width:1px' bordercolor='#003399' bordercolorlight='#c0c0c0' bordercolordark='#6699CC'>"; 
	
	
	for (var j=1,y=0;y<mm[5]-GRID_SIZE;y+=GRID_SIZE) 
	{
		html += "<tr>";
		for (var x=0;x<mm[4]-GRID_SIZE;x+=GRID_SIZE) 
		{
			  html += "<td width='"+(GRID_SIZE-2)+"' height='"+(GRID_SIZE)+"'>&nbsp;</td>"; 
		}
		delta_x = mm[4]-x;
		html += "<td width='"+delta_x+"' height='"+(GRID_SIZE)+"'>&nbsp;</td>"; 
		html += "<td>&nbsp;"+(j++)+"</td>";  
		html += "</tr>";
	}
	for (i=0;i<2;i++)
	{
		delta_y = mm[5]-y;
		html += "<tr>";
		for (var k=1,x=0;x<mm[4]-GRID_SIZE;x+=GRID_SIZE) 
		{
			  html += "<td align=center width='"+(GRID_SIZE-2)+"' "+(i==0?"height='"+delta_y+"'":"")+">&nbsp;"+(i==1?k++:"")+"</td>"; 
		}
		delta_x = mm[4]-x;
		html += "<td align=center >&nbsp;"+(i==1?k++:"")+"</td>"; 
		html += "<td>&nbsp;"+(j++)+"</td>";
		html += "</tr>";
	}
	
	
	/*
	for (var i=0,f=0,r=1;f<mm[5];f+=GRID_SIZE) {
		html += "<tr>";
		
		for (var g=0,e=0;e<mm[4];e+=GRID_SIZE)  {
			html += "<td>&nbsp;</td>";  
			g=(mm[4]-e)-GRID_SIZE;
		}
		if (g>0) html += "<td><img src='index.php?i=px' width='"+(g-2)+"' height=1></td>";
		
		html += "<td><img src='index.php?i=px' height='"+(GRID_SIZE-2)+"' width=1 align=absmiddle>"+r+"</td></tr>";
		r ++;
	    i=(mm[5]-f)-GRID_SIZE;
	}
	
	if (i>0) {
		html += "<tr>";
		for (var e=0;e<mm[4];e+=e+(GRID_SIZE*2)>=mm[4]?(mm[4]-e):GRID_SIZE) {
			html += "<td><img src='index.php?i=px' height='"+(i-2)+"' width=1></td>";
			g=mm[4]-e;
		}
		if (g>0) html += "<td></td>";	
		html += "<td>"+r+"</td></tr>";
	}
	
	html += "<tr>";
	for (var e=0,c=1;e<mm[4];e+=e+(GRID_SIZE*2)>=mm[4]?(mm[4]-e):GRID_SIZE) {
		 html += "<td align=center><img src='index.php?i=px' height=1 width='"+(GRID_SIZE-2)+"'><br>"+c+"</td>";  
		 c++;
		 g=mm[4]-e;
	}
	if (g>0) html += "<td></td>";	
	
	html += "<td></td></tr>"; 
	*/
	html += "<table>"
	with (E) {
		style.display = "inline";
		style.left    = mm[0];
		style.top     = mm[1];
		if (document.all) style.filter  = "alpha(opacity=100)";
		innerHTML=html;
	}
} // render_drawing_grid



function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
} 

// mozilla resize stuff
function desktop_background_resize () { // desktop_background_resize
  // if (document.all) return false;
   if (EDITMODE=="e") WINDOWOFFSET = 10;
   if (ISPDA) return false;
   try
   {
		var Dd = document.getElementsByTagName("DIV");   
		 
		
		for (var i=0;i<Dd.length;i++)  {	 
			if (Dd[i].className&&Dd[i].className=="resizeX") {
				Dd[i].style.width  = ( (document.all?document.body.offsetWidth:window.innerWidth)   - docx(Dd[i]) ) - 10; 
			}
			if (Dd[i].className&&Dd[i].className=="resizeY") {
				Dd[i].style.height = ( (document.all?document.body.offsetHeight:window.innerHeight) - docy(Dd[i])) - 10;  
			}
			if (Dd[i].className&&(Dd[i].className=="resizeXY")) {    
				Dd[i].style.height = ( (document.all?document.body.offsetHeight:window.innerHeight) - docy(Dd[i]) ) - WINDOWOFFSET;  
				Dd[i].style.width  = ( (document.all?document.body.offsetWidth:window.innerWidth)   - docx(Dd[i])) - 10;  
			}   
			if (Dd[i].className&&(Dd[i].className=="autocomplete")) {    
				Dd[i].style.height = ( (document.all?document.body.offsetHeight:window.innerHeight) - docy(Dd[i]) ) - WINDOWOFFSET;  
				Dd[i].style.width  = ( (document.all?document.body.offsetWidth:window.innerWidth)   - docx(Dd[i])) - 10;  
			}   
		} 
   }
   catch (ex) 
   {
	    // do nothing
   }
} // desktop_background_resize 

var SNAPSHOT = true
function snapshot () {
	var d=MM_findObj,S=d("statpanel"); 
	// if (!S) S=d("wsborder");
	var Dd = document.getElementsByTagName("div");  
	var Td = document.getElementsByTagName("table");   
	var Di = document.getElementsByTagName("img");   
	var Da = document.getElementsByTagName("a");   
	html = "<style>\nbody, td {font:7pt verdana}\n</style>";
	html += "<div style='color:gray;border:solid 1px red;position:absolute;background-color:whitesmoke;";
	html += "left:0;top:0;width:"+document.body.offsetWidth+";height:"+document.body.offsetHeight+";' align=right></div>\n";
	
	var k=new Array();
	k["div"]   = new Array ("firebrick", "red", "left",false);
	k["table"] = new Array ("blue", "slateblue", "right",false);
	k["img"]   = new Array ("green", "lime", "left",false);
	k["a"]     = new Array ("gray", "silver", "left",true);
	
	for (I in k) {
		 Dd = document.getElementsByTagName(I);  
		for (var i=0;i<Dd.length;i++)  {	 
		   m=mboundary(Dd[i]) 
		   
		   if (m[4]>0&&m[5]>0) {
			   if (Dd[i].id) {    
			        N=k[I][3]?Dd[i].innerHTML:Dd[i].id;
					if (I=="img") N="<img src='"+Dd[i].src+"'>";
					html += "<div title='"+I+": "+Dd[i].id+" ("+m[0]+"x"+m[1]+" "+m[4]+"x"+m[5]+")' style='color:"+k[I][0]+";border:dotted 1px "+k[I][0]+";position:absolute;";
					html += "left:"+m[0]+";top:"+m[1]+";width:"+m[4]+";height:"+m[5]+";overflow:hidden' align="+k[I][2]+">"+N+"</div>\n";
			   } else {
			        N=k[I][3]?Dd[i].innerHTML:"";
					html += "<div title='"+I+": NO ID ("+m[0]+"x"+m[1]+" "+m[4]+"x"+m[5]+")' style='color:"+k[I][1]+";border:dotted 1px "+k[I][1]+";position:absolute;";
					html += "left:"+m[0]+";top:"+m[1]+";width:"+m[4]+";height:"+m[5]+";overflow:hidden' align="+k[I][2]+">"+N+"</div>\n";
			   }
		   }
		}  
	} 
	 
	html += "<div style='color:green;border:dotted 1px green;position:absolute;background-color:whitesmoke;";
	html += "left:0;top:"+document.body.offsetHeight+";padding:4px'>";
	if (page_properties) {
		for (P in page_properties) html += P+": "+page_properties[P]+"<br>";
	}	
	html += "</div>\n";
	
	
	var k=window.open ("about:blank");
	k.document.write (html); 
}

function addglobal (n,v) {
	/*   var a=new Array();
	   for (var i=0;i<GLOBALS.length;i+=2)  {
		   if (GLOBALS[i]!=n) {
			   a[a.length]=GLOBALS[i];
			   a[a.length]=GLOBALS[i+1];
		   }
	   }
	   GLOBALS=a;
	GLOBALS[GLOBALS.length] = n;
	GLOBALS[GLOBALS.length] = v;*/
	try {
		if (page_properties==null)
	        page_properties = pagevars(); 
		 
		    page_properties[n]=v;
	} catch (ex) { alert ("addglobal error: "+ex.message) }
}

// function fb_edit -----------------------------------------------------'
function fb_edit() {
// ----------------------------------------------------------------------'   
    var n,q,s,b,h,a,fb=fb_edit.arguments,f=new Array(); 
	for (P in page_properties) f.push(P,page_properties[P]);
    for (var k=0;k<fb.length;k++) f.push(fb[k]); 
   
    h = location.href.split("?"); 
    if (h[1]) {      
        a = h[1];
        for (var k=0;k<f.length;k+=2) {
			s = "";
			b = false;
            q = a.split("&");
			for (var i=0;i<q.length;i++) {
			    n = q[i].split("=");
			    if (n[0]==f[k]) {
			        s += (s==""?"":"&")+f[k]+"="+f[k+1];
			        b = true;
			    } else s += (s==""?"":"&")+q[i];
			}
			
			if (!b) s += (s==""?"":"&")+f[k]+"="+f[k+1];
			a = s; 
        }                
    } else { 
        s = "";
        for (var k=0;k<f.length;k+=2)
            s += (s==""?"":"&")+f[k]+"="+f[k+1];  
    } 
    try {
         s += "&top="+(pageform.html.scrollTop)
    } catch (ex) {    }
	
    location.href = h[0]+"?"+s;
} // fb_edit
// **********************************************************************'

function docx(e) {
	try {
        for (var x=0,i=e;i.tagName!="BODY";i=i.offsetParent) x += i.offsetLeft;
	} catch (ex) { x=0}
    return x;
} 

function docy(e) {  
	try {  
        for (var y=0,i=e;i.tagName!="BODY";i=i.offsetParent) y += i.offsetTop;
	} catch (ex) { y=0}
    return y;
} 


function mboundary (element) {
   try {
	var Px = docx(element);	
	var Py = docy(element);
    var PX = Px + element.offsetWidth;
	var PY = Py + element.offsetHeight; 
    var Pw = element.offsetWidth;
	var Ph = element.offsetHeight; 
	return new Array(Px,Py,PX,PY,Pw,Ph)
	} catch (ex) { alert ("mboundary error: "+ex.message); return new Array(0,0,0,0,0,0) }
}


function xm (e,i) { // xm (deprecated)
	 if (i=="L") return e.scrollLeft;
	 else if (i=="T") return e.scrollTop; 
	 else if (i=="W") return e.offsetWidth; 
	 else if (i=="H") return e.offsetHeight; 
	 else if (i=="X") return docx(e); 
	 else if (i=="Y") return docy(e); 
} // xm (deprecated)

function scroll_display() { 
    showstatus ("Scrolling...");
	scroll_displayex();
}
function scroll_displayex() { // scroll_display
    /* DEBUG DATA */ addtolog("scroll_display",scroll_display); 
	var d = document.all?document.all:document.getElementsByTagName("div")
	 
	if (d) {  
        f=MM_findObj;
		var DX = xm (f("Dx"),"X"); // docx(d("Dx"));
		var DY = xm (f("Dx"),"Y"); // docy(d("Dx"));
		var PH = xm (f("Dx"),"H"); // d("Dx").offsetHeight;
		var PW = xm (f("Dx"),"W"); // d("Dx").offsetWidth; 
		var Pw = xm (f("Dx"),"L"); // d("Dx").scrollLeft;
		var Ph = xm (f("Dx"),"T"); // d("Dx").scrollTop;  
		 
		for (var e=0;e<d.length;e++) {
			if (d[e].className=="preload"&&get_rollup("myopenfiles")) 
			{ 
			    di = d[e].innerHTML; 
				if (di.indexOf("cnimg")<0&&di.length>0) 
				{
					di = "thumbnail.php?sd=sd&ft=gif&w=32&i="+escape(di);   
					d[e].innerHTML = "<img class='cnimg' src='"+di+"' >"; 
				}
			}
			else if (d[e].className=="offload") 
			{ 
				
				element= d[e];
				Px = docx(element)-DX;	
				Py = docy(element)-DY;
				PX = Px+element.offsetWidth;
				PY = Py+element.offsetHeight; 
				Py-=Ph;
				PY-=Ph;
				Px-=Pw;
				PX-=Pw;
				 
								  
				di = d[e].innerHTML; 
				
			    if ((Py<PH)||(VIEWMODE!="t")) 
				{
				    if (Px<PW) 
					{
						if (di.indexOf("cnimg")<0&&di.length>0) 
						{  
							if ( ((VIEWMODE!="t")||(PY>=0))&&(PX>=0) ) 
							{
								if (di.indexOf("http:")<0)
								{
									di = "thumbnail.php?sd=sd&ft=gif&w="+(VIEWMODE=="t"?132:100)+"&i="+escape(di.indexOf(PATH)<0?PATH+di:di);   
								}
								else
								{
									di = "thumbnail.php?sd=sd&ft=gif&w="+(VIEWMODE=="t"?132:100)+"&i="+escape(di);   
								}
								html = "<img class='cnimg'";
								html += " src='"+di+"' ";
								html += " onerror=\"MM_findObj('dmsg"+e+"').innerHTML='Error loading image'\" ";
								html += " onload=\"MM_findObj('dmsg"+e+"').innerHTML=''\">";
								html += "<div style='color:green' id='dmsg"+e+"'>Building...</div>";  
								d[e].innerHTML = html;
							}  
						}
					} else if (VIEWMODE!="t") break;
				} else if (VIEWMODE=="t") break; 
			}	 
		}
	} 
	hidestatus();
} // scroll_display


function savetextform() { // savetextform
	var d=MM_findObj,D=d("html"),H=d("dhtml"),L=d("scrollL"),T=d("scrollT");
    L.value = H.scrollLeft;
    T.value = H.scrollTop;
    D.value = H.innerText;
	//alert (T.value)
	//var k = window.open();
	//k.document.write ("<xmp>" + H.innerText);
	
    this.submit()
} // savetextform


function textfile (n,p,f) {
    this._name   = n;
    this._path   = p; 
    this._title  = filetitle(n);
    this._active = f;
}

function refreshEditor () { // refreshFiles
   //  addtolog("refreshFiles",refreshFiles);
  // alert (1)
   var d=MM_findObj;
   
  // alert (2)
   var M=d("dfilelist");
  // alert (M==null)
  // alert (M.length)
	var html = ""; 
 	 
	for (var j=0;j<instance.length;j++) { 
		 if (instance[j]._active) {
			html += "<label disabled style='background-color:#CCEEFF;padding:2px;'>&nbsp;";
		 } else {
			 html += "<span style='background-color:#99CCFF;padding:2px;color:white;";
			 html += "border-left:solid 1px gainsboro;border-right:solid 1px gray;";
			 html += "border-top:solid 1px gainsboro;'>&nbsp;";
		 } 
		 html += "<a href=\"javascript:fb_edit('mfile','"+instance[j]._path+"','f','')\""; 
		 html += ">"+instance[j]._title+"</a>&nbsp;</label>"; 
		 html += "<a href=\"javascript:fb_edit('remove','"+instance[j]._path+"', 'mfile', '"+FILE+"')\"";
		 html += " style='font-family:webdings'>r</a>";  
		 html += "</span>"; 
	} 
	//alert (html)
	M.innerHTML = html;
} // refreshFiles 



function filetitle(thepath) { // filetitle
   try {
    v = thepath.split ("/");
	v = v[v.length-1];
	v = v.length>20?"..."+v.substr(v.length-20):v;
	return v;
   } catch (ex) { return ex.message }
} // filetitle



function avpreview () {
     /* DEBUG DATA */ addtolog("avpreview",avpreview);
	var d=MM_findObj,MP=d("mainpic"),EW=d("dworkspace"),DR=d("dprevpic");
	boundP = mboundary (MP); // image boundary
	boundV = mboundary (EW); // view area boundary
	outerW = (boundP[2]-boundP[0]) > (boundV[2]-boundV[0]);
	outerH = (boundP[3]-boundP[1]) > (boundV[3]-boundV[1]);
	DR.style.left = ((docx(EW) + EW.offsetWidth) - DR.offsetWidth)-(outerH?20:0)
	DR.style.top  = docy(EW) + 4;
}



function setoffset (i) {
     /* DEBUG DATA */ addtolog("setoffset",setoffset);
   if (i>16) {
       alert ("Max 1600%")
   } else {
	   // set new offset value
	   Hoffset = i;
	   
	   // reset pen size
	   if (tools[SELECTEDTOOL]._pen) setpen (tools[SELECTEDTOOL]._pen);
	   
	   // size the picture
	   sizepictoHoffset();
	   
	   // redraw the zoom nav panel
	   gozoom();
   }
}

function hidestatus() {
     /* DEBUG DATA */ addtolog("hidestatus",hidestatus);
	    var d=MM_findObj,S=d("statpanel");
    try {  
		if (!S) S=d("wsborder");
		with (S) { 
			style.display   = "none";
			innerHTML = "";
		}
	} catch (ex) { }  
}

function showstatus(message,bgcolor,fgcolor) {
     /* DEBUG DATA addtolog("showstatus",showstatus);*/ 
	 if (ISPDA) return false;
    try {    
	    var d=MM_findObj,D=d("dfilelist"),S=d("statpanel");
		// var st = mboundary (D);
		if (!S) S=d("wsborder");
		with (S) {
			style.display           = "inline";
			style.backgroundColor   = bgcolor||"#FF0099";
			style.color             = fgcolor||"#FFFFFF";
			innerHTML               = message;
			style.left              = 0; 
			style.width             = (document.all?document.body.offsetWidth:window.innerWidth)-2;
			style.top               = -2+(document.all?document.body.offsetHeight:window.innerHeight)-offsetHeight;
		}
	} catch (ex) { }
}


var xmlHttp = XmlHttp.create(); 
function wsdl(request) { 
     /* DEBUG DATA */ addtolog("wsdl",wsdl);
   showstatus ("Sending...")    
    return wsdlex(request)
}

function wsdlex(R,url) {  
	/* DEBUG DATA */ addtolog("wsdlex",wsdlex);   
	var ret,A=url||"thumbnail.php"; 
	er=0;
	try {   
		xmlHttp.open("GET", A+"?"+R, false); er=1;
		xmlHttp.send(R); er=2;
		ret = xmlHttp.responseText;  er=3; 
	} catch (ex) { ret = er+". wsdlex error: "+ex.message } 
	showstatus ("Response received. Processing...")    
	return ret 
} 

// debug scripts
var debugwindow = false;
var debugdata   = new Array();
var commands    = new Array();
var contents    = new Array();

function addtolog (title,content) {  /*
	var html = "";
	commands[commands.length] = title;
	contents[contents.length] = content;
	start = commands.length-10;
	for (var k=start>0?start:0;k<commands.length;k++) {
		args="";
		html += "<font color=blue>"+k+". "+commands[k]+"</font><br>"; 
		f=contents[k];
		try {
			for (var z=0;z<f.arguments.length;z++) {
				 args += (args==""?"":",")+f.arguments[z];
			}
		} catch (ex) { }
		if (args!="") html += "<small>("+args+")</small><br>"; 
	}
	var d=MM_findObj;
	if (d("ddebug"))
	    d("ddebug").innerHTML = html;
	else  
	   alert ("No debug");
	return false;
	try {
		debugwindow.blur();
		debugwindow.additem (title,content);
	} catch(ex) {
        debugdata = new Array();
		d("debug").innerHTML = title
		 opendebug ();
	}
	*/
}
 
function opendebug () { /*
    try {
		var html="<html>";
		html += "<style>\nbody,td{font:8pt tahoma}"; 
		html += "</style>\n"; 
		html += "<body style='border:solid 1px buttonface' bgcolor='buttonface' >"; 
		html += "Last Call<input size=40 id=ilastcall>";
		html += "<div id='dlist' style='width:100%;background-color:white;height:300;overflow:auto;border:inset 1px'>"; 
		html += "</div>";
		html += "</html>";
		debugwindow = window.open ("about:blank",null,"width=600,height=340");
		debugwindow.document.write (html);
		debugwindow.opener = self; 
		
		debugwindow.additem = function (title,content) {
		  var d=new debugpoint(title,content);
		  
		  if (content.caller) {
				 cs = content.caller.toString() 
				 if (cs.indexOf("{")>0) cs = cs.substr( 0,cs.indexOf("{") );
			   d._caller =   cs
		   }
		 //  d._arguments = content.arguments;
			debugdata[debugdata.length] = d;
			var html = "<table width=100% cellpadding=2 cellspacing=1 bgcolor=green>"
			for (var z=debugdata.length-1;z>=0;z--) {
				 html += "<tr>"
				 html += "<td bgcolor=white>"
				 html += z+". <u style=color:firebrick>"+debugdata[z]._title + "</u>(";
				 a="";
				 for (var k=0;k<debugdata[z]._arguments.length;k++) {
					 a+= (a==""?"":",")+debugdata[z]._arguments[k];
				 }
				 html += a+")<br>"+debugdata[z]._caller;
				 html += "</td>"
				 html += "</tr>"
			}
			html += "</table>";
			debugwindow.ilastcall.value = title;
			debugwindow.dlist.innerHTML = html;
						
		}
	} catch (ex) { alert ("opendebug: "+ex.message) }*/
}

function clean(i) { // clean
    return i.replace(/>/g,"&gt;").replace(/</g,"&lt;")
} // clean

function debugpoint(t,f,c) { // debugpoint
    var args = new Array();
	this._title    = t;
	this._function = f;
	for (var z=0;z<f.arguments.length;z++) args[z] = f.arguments[z]; 
	this._arguments = args;
	this._caller   = c;
} // debugpoint  

function locatesplitters () {
	var Dd = document.getElementsByTagName("DIV");   
	for (var e=0;e<Dd.length;e++) {
		 if (Dd[e].className.indexOf("splitter")==0) {
			   locatesplitter(Dd[e].id)
		 }
	}	
}

function getParent() { // getParent
     gofolder (PARENT);
} // getParent

function goimage (a) { // goimage
    var d = MM_findObj;
    fb_edit ('f',a,'s',d("Dx").scrollLeft,'t',d("Dx").scrollTop,'w',d("Dx").offsetWidth) 
} // goimage

function navigate (e) {
    gofolder(e);
}


function gofolder (a) { // gofolder
	try {  
	    
	    // be sure to exit TEXT EDIT mode
		if (!page_properties) page_properties = pagevars();
		if (page_properties["e"]&&page_properties["e"]=="t") addglobal("e","b"); 
		
		// clear any file data from query string
		addglobal("mfile",""); 
		
	    // for remote folders, navigate to the same folder with the remote folder set	
	    if (DIRNAME.indexOf("prem")>0) fb_edit('d', DIRNAME, 'rd', a, 'f', '', 's', '')   
		else {
			var d = MM_findObj,w=d("Dx")
			if (w) {
				W=w?w.offsetWidth:0;
				addglobal ('w',W); 
			}
			if (ISPDA) addglobal ('fo', ''); 
			fb_edit ('d', a, 'f', '', 's', '', 'rd', ''); 
		}
	} catch (ex) { alert (ex.message) } 
}  // gofolder

function openslides () { // openslides
    var h = location.href.split ("?");
    window.open(h[0]+'?l=l&h=h&d='+DIRNAME, 'slideshow', 'resizable=yes,menubar=no')
} // openslides 


function chmodnum(mode) {
	
   var n=new Array()
   for (var k=0;k<mode.length;k++) {
	   c=mode.substr(k,1);
	   m="";
	   if (c=="-") m="0";
	   else if (c=="r") m="4";
	   else if (c=="w") m="2";
	   else if (c=="x") m="1";
	   n[n.length]=parseInt(m);
   }
 
   newmode = '';
   newmode += n[0]+n[1]+n[2];
   newmode += n[3]+n[4]+n[5];
   newmode += n[6]+n[7]+n[8];
    
	for (;newmode.length<4;newmode=newmode.toString())
	   newmode="0"+newmode;
   return newmode;
}


function parsemode(k,j) {
	var D=document.all(k),h="";
	// alert (1)
	// alert (D.length)
	for (d=0;d<D.length;d++) {
		h+= D[d].checked?D[d].value:"-";
	}
	H = chmodnum(h)
	MM_findObj(j+"dec").value = H;
	MM_findObj(j).value = parseInt(H,8); 
}

function pagevars() {
	var n,nv=new Array(),h=location.href.split ("?");
	if (h[1]) {
		q = h[1].split("&");
		for (var i=0;i<q.length;i++) {
			n = q[i].split("=");
			// nv[nv.length] = n[1];
			nv[n[0]] = n[1]; 
		}
	}
	return nv;
}

function rollups () {
	var Dd = document.getElementsByTagName("TABLE");   
    try { 
		var cc = pagevars(); 
		// alert (cc);
		for(var e=0;e<Dd.length;e++) {
			var x = Dd[e].id;
			if (cc[x]) {
				// alert (cc[x])
				if (cc[x]==0) rollup(x)
			}
		}
   } catch (er) { alert (er.message) }
}

function get_rollup(t) 
{ // get_rollup
   var c=MM_findObj;d=c(t);
   return ! ( d.rows[1].style.display=="none" );
}

function rollup(t,i) { // rollup
   // if (!document.all) return false
   var c=MM_findObj;d=c(t),e=c('ck'+t),m=c('im'+t),s=c('sp'+t),z=i||1;
   var b=d.rows[z].style.display=="none";
   d.rows[z].style.display = b?"inline":"none";z++;   
   if (e) e.checked=b;
   if (m) m.src=b?"index.php?i=o":"index.php?i=p";
   if (s) { 
       if (s.length) {
           for(e=0;e<s.length;e++) s[e].height=s[e].offsetParent.offsetHeight; 
       } else s.height=s.offsetParent.offsetHeight; 
   }

 //  d.rows[0].bgColor = b?"aliceblue":"white";
   if (z<d.rows.length) window.setTimeout ("rollup('"+t+"',"+z+")",50);
   else if (d._expand) d._expand()
   else addglobal (t,b?1:0);
   try { gozoom(); } catch (ex) { }
} // rollup

<!-- image size control -->
function cachedimage (x,w,h) { // cachedimage
   this._src     =x;
   this.width    =w;
   this.height   =h
   this._onload  = sizepicend;
   this._refresh = updatesize;
} // cachedimage

function updatesize() { // updatesize
	var i = "i="+IMAGEPATH+"&ac=ac";
	actual = wsdl(i); 
	actual = actual.split(","); 
	this.width  = actual[0];
	this.height = actual[1];
} // updatesize

function sizepic () { // starts resize process
	/* DEBUG DATA */ addtolog("sizepic",sizepic);
	// alert (1);
	if (ISPDA) return false;
	var d=MM_findObj,MP=d("mainpic");
	try { 
		if (DORESIZE) {
	// alert (2);
			MP.onload = null; 
			showstatus("Loading "+FILE+" into image processor...");
			window.setTimeout ("sizepicstart ()", 100);
		} else {
	// alert (3);
			MP.src = "thumbnail.php?h=X&i="+FILE; 
		    desktop_background_resize();
		} 
	} catch (ex) { 
	// alert (4);
	    window.status = ("sizepic error: " + ex.message);
		if (page_properties==null) scroll_reset();
		desktop_background_resize();
	}
} // sizepic
  
function sizepicerror() 
{ 
	if (retry_time>0) {
		showstatus ('Error: '+FILE+'. Retrying in '+retry_time+'...','gold','navy');
		retry_time--;
		retry_counter=window.setTimeout ('sizepicerror()', 1000);		
	} else if (retry_attempt<4) { 
		retry_attempt++;
		sizepic();
	} else { 
		showstatus ('Failed to load '+FILE+' after '+retry_attempt+' attempts','red','white');
		retry_counter=null;
		retry_attempt=0;
		retry_time=5;
	}
} 

var retry_counter=null,retry_time=5,retry_attempt=0;
function sizepicerrorex () { // sizepicerror
    showstatus(""+FILE+" failed to load!");
    // document.title = ""+IMAGEPATH+" failed to load!";
} // sizepicerror

function sizepicstart () { // loads a full-sized image to get the size
     /* DEBUG DATA */ addtolog("sizepicstart",sizepicstart);
	// document.title = "Reading "+FILE+"...";
	if (CACHEDIMAGE==null) {
		try {    
	//alert (14);
			showstatus("<a href='javascript:sizepic()'>Reading "+FILE+". Click <u>here</u> if the image fails to load.</a>..."); 
						 
			if (EDITMODE=="e") {
	//alert (15);
			
				CACHEDIMAGE = new cachedimage();
				CACHEDIMAGE._refresh();
				CACHEDIMAGE._onload(); 
 
			    refreshFiles (); // draw the file list
			    loadZoom (); // draw zoom window
			} else sizepicend();
			
        } catch (ex) { alert ("sizepicstart error: "+ex.message) }
			
	} else {
	//alert (16);
		showstatus("<a href='javascript:sizepic()'>Reloading "+FILE+". Click <u>here</u> if the image fails to load.</a>...");
		CACHEDIMAGE._onload();
	}
}

function sizepictoHoffset() { // sizepictoHoffset
	var d=MM_findObj,ZI=d("zoomind"),ZV=d("zoomval"),MP=d("mainpic"),DW=d("dworkspace"),EW=d("editworkspace"),WB=d("wsborder")
	var H = actual[1]*Hoffset; 
	var R = Hoffset*100;  
	try {  
		if (ZI) ZI.src   = "thumbnail.php?b=scaler&p="+R
		if (ZV) ZV.value = R+"%";  
	} catch (ez) { alert ("error loading zoom window: "+ez.message) }
	
	// resize image
	MP.height = H;
	MP.width  = actual[0]*Hoffset; 
	
	// store image path
	imageurl = "i="+IMAGEPATH +"&h="+H;   
	
	// move workspace over image ------------------------------------------------------'
	boundP = mboundary (MP); // image boundary
	boundV = mboundary (DW); // view area boundary
	outerW = (boundP[2]-boundP[0]) > (boundV[2]-boundV[0]);
	outerH = (boundP[3]-boundP[1]) > (boundV[3]-boundV[1]);
	borders = new Array(EW,WB);
	for (var v=0;v<2;v++) {
		if (borders[v]) {
			with (borders[v]) {
				style.left   = docx(MP);
				style.top    = docy(MP);
				style.width  = outerW?DW.offsetWidth-(outerH?20:0):MP.offsetWidth;
				style.height = outerH?DW.offsetHeight-(outerW?20:0):MP.offsetHeight;
				if (v==0&&document.all) style.backgroundColor="white"; 
			}
		}
	} // move workspace over image ----------------------------------------------------'	
} // sizepictoHoffset

var Hoffset = 1; 
function sizepicend () { // fires when resizer image finishes loading 
     /* DEBUG DATA */ addtolog("sizepicend",sizepicend);

	var d=MM_findObj,MP=d("mainpic"),ZR=d("prevpic"),DR=d("dprevpic"),DW=d("dworkspace"); 
	showstatus("<a href='javascript:sizepic()' style='color:yellow'>Downloading "+filetitle(FILE)+"</a>...");
	var timeindex = new Date()
    timeindex = timeindex.getMilliseconds()
	
	MP.onerror = sizepicerror;
	
	
	// attach main image onload
	MP.onload = function() { // MP.onload
		document.title = "ThePIMP - "+FILE;
		
		hidestatus(); 
	     desktop_background_resize () ;
		// move any splitters into place
		 locatesplitters ();
		
		if (page_properties==null) 
		{
			scroll_reset();
		}
		if (GRIDFLAG=="on") 
		{
			render_drawing_grid();
		}
		if (EDITMODE=="e") 
		{  // if (EDITMODE=="e") 
		    showstatus("<a href='javascript:sizepic()' "+FILE+" loaded. Please wait</a>...");
	        var ZP=d("zoompic");
		   
		   
		   // move brushes into place
		   loadtracersex (MAXBRUSHES,BRUSH_RELOCATE);  
			   
		    sizepictoHoffset();  
			
		    // attach zoom window onload
		    ZP.onload = function() 
			{
			   // draw zoom navigation panel
			   gozoom(); 
			   
			   
			   showstatus("Loading history...");
			   // reset history mode to last place
			   if (historymode>1) historymode=-3
			  
			   // refresh history panel
			   gethistory()
		    }  
		   
		    // attach thumbnail window onload
		    ZR.onload = function () 
			{
			   showstatus("Loading zoom window...");
			   
		       // locate thumbnail window
			   avpreview(); 
			   
			   // load zoom window
			   if (get_rollup("ztable"))
			   { // only load the image if the panel is shown
				   ZP.src="thumbnail.php?i="+IMAGEPATH+"&sd=sd&w=140&ze="+timeindex;
			   }
			   else
			   { // otherwise call the image's onload method
				   ZP.onload();
			   }
			   
		    }
		  
		    // load thumbnail window
		    showstatus("Loading thumbnail window...");
			if (get_rollup("avtable"))
			{ // only load the image if the panel is shown
		        ZR.src="thumbnail.php?fo=100&sd=sd&bg=CC3399&ft=png&w=132&i="+IMAGEPATH+"&ze="+timeindex;  
			} 
			else
			{ // otherwise call the image's onload method
				ZR.onload();
			}
		     
		    // scroll if zooming
		    if(gx>0) 
			{
			    DW.scrollLeft = (gx*2)-(DW.offsetWidth/2);
			    DW.scrollTop  = (gy*2)-(DW.offsetHeight/2);
		    }   
		  
		    // fire selected tool's _after event
            if (tools[SELECTEDTOOL]._after) tools[SELECTEDTOOL]._after(); 
			
		  
		}  // if (EDITMODE=="e")  
		
	 } // MP.onload   
	
	 try 
	 {
		 
	   if ((EDITMODE=="e")||DORESIZE) 
	   {
	      if (EDITMODE=="e") 
		  {
			  H=actual[1];
		  }
		  else if ((VIEWMODE=="t")||(VIEWMODE=="d")||ISADMIN) 
		  {
			  H=200;
		  }
		  else if (PREVIEWFLAG=="on") 
		  {
			  H=document.body.offsetHeight-80;
		  }
		  else 
		  {
			  H=document.body.offsetHeight-200;
		  }
		  
		  // refresh mainpic source
		  imageurl = "i="+IMAGEPATH +"&h="+H; 
		  
		  if (EDITMODE!="e")
		  {      
			  imageurl += "&sd=sd";
		  }
		  else if (SHOWMETRICS)
		  {
			  imageurl += "&em=em";
		  }
		  MP.src = "thumbnail.php?"+imageurl+"&ze="+timeindex
	   } 
	   else 
	   {
		   MP.src = "thumbnail.php?h=X&i="+FILE;  
	   }
    } 
	catch (ex) 
	{ 
	    alert("mainpic error: "+ex.message) 
	} 
} // sizepicend


if (EDITMODE=="b") { 
    if (ISMEDIA) {
	   window.onresize = function () { 
			try {
				if (VIEWMODE!="") {
					var d=MM_findObj,X=d("Dx"),W=d("infotable");  
					X.style.height = document.body.offsetHeight - (docy(W)+W.offsetHeight+10); 
			    }
			} catch (ex) { }
	   }
	} else window.onresize = sizepic;
} // if (EDITMODE=="b")


if (EDITMODE=="t") {
	
	   window.onresize = desktop_background_resize
}

if (EDITMODE=="e") {
	
	   window.onresize = desktop_background_resize
}

if (document.all) {

} else {
   window.onresize = desktop_background_resize
   if (ISMEDIA) desktop_background_resize()
}


 keymessage = "Invalid action";
function nsdenykeyaction(e) { 
	KEYCTRL=17
	KEYALT =18
	KEYF5  =116 
	if ((e.which==KEYCTRL)||(e.which==KEYALT)||(e.which==KEYF5)) {
		// alert (keymessage);
	    return false;
	}
}

function drawspt(eX,eY,eT) {  
     if (splitter==null) return false;
	var X=eX,Y=eY,E=eT;
	if (document.all) {
		X = window.event.x;
		Y = window.event.y; 
		E = event.srcElement; 
	}  
	 
		mw = X-spx;
		mh = Y-spy;
		if (spd=="Y") splitter.style.left = mw;
		else  splitter.style.top = mh;
}

function showspt() {  
     if (splitter==null) return false;
	A=splitter.className.split ("_");
	M=MM_findObj (A[1]);
	new_width  = docx(splitter) - docx(M);
	new_height = M.offsetHeight - (docy(splitter) - docy(M));
	if (spd=="Y") {
		 M.style.width = new_width;
		 addglobal ("sw", new_width);
	} else  {
		 M.style.height = new_height;
		 M.style.top    = docy(splitter);
		 addglobal ("sh", new_height);
	}
	splitter.releaseCapture();
	splitter = null;
	spy=0;
	spx=0;
	spd=0;
	desktop_background_resize ();
}

function locatesplitter (key) {
	var E=MM_findObj (key); 
	if (E.className) {
		A=E.className.split ("_");
		if (A[0]=="splitter") {
			
			
		   if (A[1]) {
			   Di = MM_findObj(A[1]); 
			   if (Di) {
				   Dm = mboundary (Di);
				   if (A[2]=="Y") {
					   mx = Dm[2];
					   my = Dm[1];
					   mw = 6;
					   mh = Dm[5];
				   } else {
					   mx = Dm[0];
					   my = Dm[1];
					   mw = Dm[4];
					   mh = 6;
				   }
				   E.style.left   = mx;
				   E.style.top    = my;
				   E.style.width  = mw;
				   E.style.height = mh;
				//   break;
			   }
		   }  
		}
	}
}

var spx=0,spy=0,spd=0,splitter=null;
function startspt(eX,eY,eT) {  
	var X=eX,Y=eY,E=eT;
	if (document.all) {
		X = window.event.x;
		Y = window.event.y; 
		E = event.srcElement; 
	} 
	if (E.className) {
		A=E.className.split ("_");
		
		if (A[0]=="splitter") {
			locatesplitter (E.id);			
			
			splitter = E;
			splitter.setCapture();
			spx = X-docx(splitter);
			spy = Y-docy(splitter);
			spd = A[2];
		}
	}
}

function nsdenymouseaction(e) { 
	if (e.button==2||e.button==3) {
		// alert (keymessage);
		 return false;
	} else nsstartspt(e);
}

if (document.all) {	 
	document.onmousedown = startspt; 
	//document.onmousemove = drawspt; 
	document.onmouseup   = showspt;  
} else {    
	document.onmouseup   = nsshowspt;
	document.onmousemove = nsdrawspt;
	document.onmousedown = nsdenymouseaction; 
	document.onkeydown   = nsdenykeyaction; 
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP | Event.KEYUP);    
}


function nsstartspt(e) {   
	startspt (e.pageX ,e.pageY ,e.target )  
}

function nsdrawspt(e) { 
	drawspt (e.pageX ,e.pageY);   
}

function nsshowspt(e) { 
	showspt (e.pageX ,e.pageY);
}

 


document.oncontextmenu = function () {
		// alert (keymessage);
		// if (!ISPDA) return false;
}
